var app = getApp();
let jumpurl=app.jumpurl.url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    amount:[
      {
              numed:"100",
              givement:"20",
              id:'0'
            },{
              numed: "200",
              givement: "40",
              id:'1'
            }, {
              numed: "300",
              givement: "60",
              id:'2'
            },
      {
        numed: "100",
        givement: "20",
        id: '0'
      },
      {
        numed: "100",
        givement: "20",
        id: '0'
      },
      {
        numed: "100",
        givement: "20",
        id: '0'
      },
      {
        numed: "100",
        givement: "20",
        id: '0'
      }
            ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var jumpurl = app.jumpurl.url;
    wx.request({
      url: jumpurl + 'chongzhi/num',
      data: {
        shopid:app.userList.shopid
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded" //post
      },
      success: function (res) {
        console.log(res);
        that.setData({
          amount: res.data.message,
          less:res.data.message[0].id
        });
      },
      
    })
  },

  /***确认支付***************/
  comfirmPay:function(e){
    var that = this;
    wx.request({
      url: jumpurl + 'chongzhi/index',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      data: {
        openid: app.userList.openid,
        chongzhiid: e.currentTarget.dataset.id,
      },
      success: function (res) {

        if (res.data.state == 1) {
          that.setData({
            order_no: res.data.out_trade_no,
          })
          // --------- 订单生成成功，发起支付请求 ------------------
          wx.requestPayment({
            timeStamp: res.data.timeStamp,
            nonceStr: res.data.nonceStr,   //字符串随机数
            package: res.data.package,
            signType: res.data.signType,
            paySign: res.data.paySign,
            'success': function (res) {
              //requestPayment:ok==>调用支付成功
              if (res.errMsg == "requestPayment:ok") {
                wx.request({
                  url: jumpurl + 'chongzhi/notify',
                  header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                  },
                  method: "POST",
                  data: {
                    order_no: that.data.order_no,
                    openid:app.userList.openid,
                  },
                  success: function (res) {
                    if(res.data == 1){
                      wx.showToast({
                        title: '充值成功',
                        icon: 'none',
                        image: '',
                        duration: 2000,
                        mask: true,
                        success: function (res) {

                          setTimeout(function () {
                            that.onLoad();
                          }, 2000);
                          setTimeout(function () {

                          }, 1000)
                        },
                      })
                    }
                  }
                })
              }
            },
            'fail': function (res) {
              console.log(res);
            },
            'complete': function (res) {
              console.log(res);
            }
          })
        } else if (res.data.state == 0) {
          wx.showToast({
            title: res.data.Msg,
            icon: 'fail',
            duration: 1000
          })
        } else {
          wx.showToast({
            title: '付款成功',
            icon: 'fail',
            duration: 1000
          })
         

        }
      },
      fail:function(err){
        console.log("调用支付失败"+err)
      }
    })
     
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },


  Chose:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    var less = id;
    that.setData({
      less: id
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },

  pays:function(){

  }


})